package com.ruoyi.xdata.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.xdata.domain.XdataModel;
import com.ruoyi.xdata.service.IXdataModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.xdata.mapper.XdataPipeMapper;
import com.ruoyi.xdata.domain.XdataPipe;
import com.ruoyi.xdata.service.IXdataPipeService;

import javax.annotation.PostConstruct;

/**
 * 通道Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-03-29
 */
@Service
public class XdataPipeServiceImpl implements IXdataPipeService 
{
    public static final String CACHE_MAP = "xdata_pipe";
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IXdataModelService xdataModelService;
    @Autowired
    private XdataPipeMapper xdataPipeMapper;


    /**
     * 缓存
     * @return
     */
    @PostConstruct
    private int reRecache(){
        redisCache.deleteObject(CACHE_MAP);
        List<XdataPipe> list = xdataPipeMapper.selectXdataPipeList(new XdataPipe());
        for(XdataPipe pipe:list){
            redisCache.setCacheMapValue(CACHE_MAP, "id_"+pipe.getId(), pipe);
        }
        return list.size();
    }

    /**
     * 查询通道
     * 
     * @param id 通道主键
     * @return 通道
     */
    @Override
    public XdataPipe selectXdataPipeById(Long id)
    {
        XdataPipe xdataPipe = redisCache.getCacheMapValue(CACHE_MAP, "id_"+id);
        if(xdataPipe != null){
            return xdataPipe;
        }
        return xdataPipeMapper.selectXdataPipeById(id);
    }

    /**
     * 查询通道列表
     * 
     * @param xdataPipe 通道
     * @return 通道
     */
    @Override
    public List<XdataPipe> selectXdataPipeList(XdataPipe xdataPipe)
    {
        return xdataPipeMapper.selectXdataPipeList(xdataPipe);
    }

    /**
     * 新增通道
     * 
     * @param xdataPipe 通道
     * @return 结果
     */
    @Override
    public int insertXdataPipe(XdataPipe xdataPipe)
    {
        autoSql(xdataPipe);
        return xdataPipeMapper.insertXdataPipe(xdataPipe) + reRecache();
    }

    /**
     * 修改通道
     * 
     * @param xdataPipe 通道
     * @return 结果
     */
    @Override
    public int updateXdataPipe(XdataPipe xdataPipe)
    {
        autoSql(xdataPipe);
        return xdataPipeMapper.updateXdataPipe(xdataPipe) + reRecache();
    }

    /**
     * 自动生成SQL
     * @param xdataPipe
     */
    private void autoSql(XdataPipe xdataPipe){
        XdataModel model = xdataModelService.selectXdataModelById(xdataPipe.getModelId());
        StringBuilder sb = new StringBuilder();
        //读数据
        if(StringUtils.isEmpty(xdataPipe.getReadSql())){
            sb.append("select ");
            for(String f:model.getFields()){
                sb.append(f).append(",");
            }
            sb.deleteCharAt(sb.length()  - 1).append(" from ").append(model.getTables()).append(" where update_time > #{_last} limit #{_page} , #{_rows}");
            xdataPipe.setReadSql(sb.toString());
        }

        /**
         * 新增数据
         */
        if(StringUtils.isEmpty(xdataPipe.getInsertSql())){
            sb.setLength(0);
            sb.append("insert into ").append(model.getTables()).append(" ( ");
            for(String f:model.getFields()){
                sb.append(f).append(",");
            }
            sb.deleteCharAt(sb.length()  - 1).append(" ) values ( ");
            for(String f:model.getFields()){
                sb.append("#{").append(f).append("},");
            }
            sb.deleteCharAt(sb.length()  - 1).append(" )");
            xdataPipe.setInsertSql(sb.toString());
        }

        /**
         * 更新数据
         */
        if(StringUtils.isEmpty(xdataPipe.getUpdateSql())){
            sb.setLength(0);
            sb.append("update ").append(model.getTables()).append(" set ");
            for(String f:model.getFields()){
                sb.append(f).append("=").append("#{").append(f).append("},");
            }
            sb.deleteCharAt(sb.length()  - 1).append(" where id = #{id}");
            xdataPipe.setUpdateSql(sb.toString());
        }

        /**
         * 验证是否存在
         */
        if(StringUtils.isEmpty(xdataPipe.getExistSql())){
            sb.setLength(0);
            sb.append("select * from ").append(model.getTables()).append(" where id = #{id} limit 1");
            xdataPipe.setExistSql(sb.toString());
        }
    }

    /**
     * 批量删除通道
     * 
     * @param ids 需要删除的通道主键
     * @return 结果
     */
    @Override
    public int deleteXdataPipeByIds(Long[] ids)
    {
        return xdataPipeMapper.deleteXdataPipeByIds(ids) + reRecache();
    }

    /**
     * 删除通道信息
     * 
     * @param id 通道主键
     * @return 结果
     */
    @Override
    public int deleteXdataPipeById(Long id)
    {
        return xdataPipeMapper.deleteXdataPipeById(id) + reRecache();
    }
}
